<?php
class GearmanCommand extends BaseCommand
{
    public function actionStart()
    {
        $jobs = require(Yii::getPathOfAlias('application.config').'/gearman.php');
        $queue = new ARedisList('gearman_queue');
        while(true)
        {
            $job = $queue->shift();
            if($job !== false)
            {
                $job = json_decode($job, true);
                $jobId = $job['id'];
                $params = new GearmanParams($job['params']);

                $worker = $jobs[$jobId][ENUM_GEARMAN_WORKER].'Worker';
                $action = $jobs[$jobId][ENUM_GEARMAN_ACTION];
                call_user_func(array($worker, $action), $params);

                $params = json_encode(unserialize($job['params']));
                echo "\n", sprintf('%s::%s(%s)', $worker, $action, $params), "\n";
            }

            usleep(5000);
        }
    }
}

class GearmanParams
{
    public $params;

    public function __construct($params)
    {
        $this->params = $params;
    }

    public function workload()
    {
        return $this->params;
    }
}